import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

public class Solution424 {
    public int characterReplacement(String s, int k) {
        Map<Character,Integer> map=new HashMap<>();
        int ans=0;
        PriorityQueue<int[]> priorityQueue=new PriorityQueue<>((a,b)->b[1]-a[1]);
        int j=0;
        for (int i=0;i<s.length();i++){
            map.put(s.charAt(i),map.getOrDefault(s.charAt(i),0)+1);
            priorityQueue.offer(new int[]{s.charAt(i)-'A',map.get(s.charAt(i))});
            while (priorityQueue.peek()[1]!=map.get((char)(priorityQueue.peek()[0]+'A'))){
                priorityQueue.poll();
            }
            while (priorityQueue.peek()[1]+k<=i-j){
                map.put(s.charAt(j),map.get(s.charAt(j))-1);
                j++;
            }
            ans=Math.max(ans,i-j+1);
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution424().characterReplacement("ABAB",2));
    }
}
